package presentation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import control.CControl;

public class PGPSPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private String mapPath = "maps/";
	private PComboBox mapChooser;
	private PPrefixText gpsDate, gpsTime, gpsSpeed, gpsTrip, gpsTotalTrip;
	private PMap mapPanel;
	private CControl cControl;
	private PButton clearButton ;
	private final int PANEL_HEIGHT = 25;

	public PGPSPanel(CControl cControl) {
		this.cControl = cControl;
		this.setBorder(new EmptyBorder(3,3,3,3));
		this.setLayout(new BorderLayout());
		this.setBackground(Color.white);
		Object[] mapList = cControl.getMaps();
		Font plainFont = new Font("Arial",Font.PLAIN,14);
		
		clearButton = new PButton("Clear", null, plainFont);
		clearButton.setPreferredSize(new Dimension(70,PANEL_HEIGHT-5));
		clearButton.addActionListener(new ButtonListener());
		
		mapChooser = new PComboBox(mapList,new ComboBoxListener(),plainFont);
		gpsDate = new PPrefixText("","--","",JLabel.RIGHT, plainFont);
		gpsTime = new PPrefixText("","--",", ",JLabel.RIGHT, plainFont);
		
		cControl.addRegisterObserver("DLN_Date", gpsDate);
		cControl.addRegisterObserver("DLN_Time", gpsTime);
		
		gpsSpeed = new PPrefixText("GPS speed: ","--",
				" " + cControl.getRegisterUnit("DLN_Speed"),JLabel.CENTER, 
				plainFont,170,PANEL_HEIGHT);
		gpsTrip = new PPrefixText("Trip: ","--",
				" " + cControl.getRegisterUnit("DLN_Trip"),JLabel.CENTER, 
				plainFont, 170,PANEL_HEIGHT);
		gpsTotalTrip = new PPrefixText("Total trip: ","--",
				" " + cControl.getRegisterUnit("DLN_TripTotal"),JLabel.CENTER, 
				plainFont,170,PANEL_HEIGHT);
		
		cControl.addRegisterObserver("DLN_Speed", gpsSpeed);
		cControl.addRegisterObserver("DLN_Trip", gpsTrip);
		cControl.addRegisterObserver("DLN_TripTotal", gpsTotalTrip);
		
		JPanel topBar = new JPanel();
		topBar.setPreferredSize(new Dimension(0,PANEL_HEIGHT));
		topBar.setBackground(Color.white);
		topBar.setLayout(new BorderLayout());
		topBar.add(mapChooser,BorderLayout.LINE_START);
		topBar.add(gpsTime,BorderLayout.CENTER);
		topBar.add(gpsDate,BorderLayout.LINE_END);
		
		this.add(topBar,BorderLayout.PAGE_START);
		
		if (mapList.length > 0) {
			String fullMapPath = cControl.getRootPath() + mapPath + mapList[0];
			mapPanel = new PMap(fullMapPath,getMapCoordinates(mapList[0].toString()),cControl);
			cControl.addRegisterObserver("DLN_GPSLat", mapPanel);
			cControl.addRegisterObserver("DLN_GPSLong", mapPanel);
			this.add(mapPanel,BorderLayout.CENTER);
		} else {
			mapPanel = new PMap("",null,cControl);
			this.add(mapPanel,BorderLayout.CENTER);
		}
		
		JPanel bottomBar = new JPanel();
		bottomBar.setPreferredSize(new Dimension(0,PANEL_HEIGHT));
		bottomBar.setBackground(Color.white);
		bottomBar.setLayout(new FlowLayout(FlowLayout.LEADING,0,3));
		bottomBar.add(gpsSpeed,BorderLayout.LINE_START);
		bottomBar.add(gpsTrip,BorderLayout.CENTER);
		bottomBar.add(gpsTotalTrip,BorderLayout.LINE_END);
		bottomBar.add(clearButton);
		
		this.add(bottomBar,BorderLayout.PAGE_END);
	}
	
	private double[] getMapCoordinates(String map) {
		String[] splitMap = map.split("[.]");
		map = map.substring(0,map.length()-(splitMap[splitMap.length-1].length()+1));
		
		double[] coordinates = new double[4];
		
		try {
			coordinates[0] = Double.parseDouble(cControl.getSettingsParameter(map + "tllat"));
		} catch (Exception e) {}
		
		try {
			coordinates[1] = Double.parseDouble(cControl.getSettingsParameter(map + "tllong"));
		} catch (Exception e) {}
		
		try {
			coordinates[2] = Double.parseDouble(cControl.getSettingsParameter(map + "brlat"));
		} catch (Exception e) {}
		
		try {
			coordinates[3] = Double.parseDouble(cControl.getSettingsParameter(map + "brlong"));
		} catch (Exception e) {}
		
		return coordinates;
	}
	
	private class ComboBoxListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent ae) {
			if (ae.getSource() == mapChooser) {
				if (mapPanel != null) {
					String fullMapPath = cControl.getRootPath() + mapPath + mapChooser.getSelectedItem();
					mapPanel.setMap(fullMapPath,getMapCoordinates(mapChooser.getSelectedItem().toString()));
				}
			}
		}
	}
	
	private class ButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent ae) {
			if (ae.getSource() == clearButton) {
				if (mapPanel != null) {
					mapPanel.clearRoute();
				}
			}
		}
	}
}
